<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <div class="login-title">“换乐汇”二手交易平台后台管理系统</div>
      <el-form :model="loginForm" class="login-form" label-width="0">
        <el-form-item prop="user_id">
          <el-input v-model="loginForm.user_id"
                    placeholder="用户ID"
                    prefix-icon="el-icon-user">

          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password"
                    v-model="loginForm.password"
                    placeholder="密码"
                    prefix-icon="el-icon-lock">

          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     class="login-button"
                     @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {useRouter} from "vue-router"
export default {
  data() {
    return {
      router:useRouter(),
      loginForm: {
        user_id:null,
        password:''
      }
    };
  },
  methods:{
    login(){
      console.log(this.loginForm);
      this.$http.post("http://localhost:8080/user/login",this.loginForm).then((res)=>{
        console.log('登录接口返回的数据：', res.data);
        if (res.data>0) {
          this.$message.success('登录成功！');
          localStorage.setItem('user_id',this.loginForm.user_id)
          this.router.push({
            path:"/Main"     //是Vue Router中的路由跳转方法，用于在应用程序中进行页面跳转
          })
        }
        else {
          this.$message.error('用户名/密码有误');
        }
      }).catch((error) => {
        console.error(error);
        this.$message.error('登录失败，请重试');
      })
    },
  },

}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f0f0;
}

.login-card {
  width: 400px;
  text-align: center;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.login-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
}

.login-button {
  width: 100%;
  border-radius: 20px;
  background-color: #409EFF;
}
</style>
